package com.example.play.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseExpandableListAdapter
import android.widget.ImageView
import android.widget.TextView
import com.example.common.entity.CommendEntity
import com.example.common.entity.CommendReply
import com.example.image.glide.GlideImgLoader
import com.example.play.R

class VideoCommentListAdapter constructor(private val list: MutableList<CommendEntity>): BaseExpandableListAdapter() {
    override fun getGroupCount(): Int = list.size

    override fun getChildrenCount(groupPosition: Int): Int = list[groupPosition].replyList.size

    override fun getGroup(groupPosition: Int): CommendEntity =  list[groupPosition]

    override fun getChild(groupPosition: Int, childPosition: Int): CommendReply =  list[groupPosition].replyList[childPosition]

    override fun getGroupId(groupPosition: Int): Long = list[groupPosition].id.toLong()

    override fun getChildId(groupPosition: Int, childPosition: Int): Long = list[groupPosition].replyList[childPosition].id.toLong()
    override fun hasStableIds(): Boolean = false

    override fun isChildSelectable(groupPosition: Int, childPosition: Int): Boolean = false

    override fun getGroupView(groupPosition: Int, isExpanded: Boolean, convertView: View?, parent: ViewGroup?): View {
        var groupEntity = list[groupPosition]


        //使用传统方式实现组件复用
        var groupView = convertView ?: LayoutInflater.from(parent?.context).inflate(R.layout.item_comment_group, parent, false).apply {
            tag = GroupViewHolder(this)
        }
        //获取GroupViewHolder对象
        var groupViewHolder = groupView.tag as GroupViewHolder
        GlideImgLoader.loadCircleImage(groupView.context, groupEntity.userlogo, groupViewHolder.ivUserIcon)
        groupViewHolder.apply {
            tvNickName.text = if (groupEntity.nickname.isNullOrEmpty()) "白雪公主" else groupEntity.nickname
            tvContent.text = groupEntity.content
            tvCreateTime.text = "发布时间：${groupEntity.createtime}"
            tvExpand.text = if (isExpanded) "——收起回复——" else "——展开回复——"
        }

        return groupView
    }

    override fun getChildView(groupPosition: Int, childPosition: Int, isLastChild: Boolean, convertView: View?, parent: ViewGroup?): View {
        val childEntity = list[groupPosition].replyList[childPosition]



        //使用传统方式实现组件复用
        var childView = convertView ?: LayoutInflater.from(parent?.context).inflate(R.layout.item_commend_child, parent, false).apply {
            tag = ChildViewHolder(this)
        }
        //获取GroupViewHolder对象
        var childViewHolder = childView.tag as ChildViewHolder
        GlideImgLoader.loadCircleImage(childView.context, childEntity.userlogo, childViewHolder.ivUserIcon)
        childViewHolder.apply {
            tvNickName.text = if (childEntity.nickname.isNullOrEmpty()) "白雪公主" else childEntity.nickname
            tvContent.text = childEntity.content
            tvReplyTime.text = "回复时间：${childEntity.createtime}"
        }

        return childView

    }

    /**
     * 管理Group组件中子UI组件
     */
    inner class GroupViewHolder(groupView: View){
        var ivUserIcon: ImageView = groupView.findViewById(R.id.civ_user_icon)
        var tvNickName: TextView = groupView.findViewById(R.id.tv_nick_name)
        var tvContent: TextView = groupView.findViewById(R.id.tv_content)
        var tvCreateTime: TextView = groupView.findViewById(R.id.tv_create_time)
        var tvExpand: TextView = groupView.findViewById(R.id.tv_expand)
    }

    /**
     * 管理子UI组件
     */
    inner class ChildViewHolder(childView: View) {
        var ivUserIcon: ImageView = childView.findViewById(R.id.civ_user_icon)
        var tvNickName: TextView = childView.findViewById(R.id.tv_nick_name)
        var tvContent: TextView = childView.findViewById(R.id.tv_content)
        var tvReplyTime: TextView = childView.findViewById(R.id.tv_reply_time)
    }
}